package com.edu.feiyu;

import jxl.Workbook;
import jxl.biff.EmptyCell;
import jxl.write.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;

/**
 * @Author changle
 * @Time 17/8/1.
 * @Desc json转变为Excel演示
 */
public class JsonToExcelDemo {
    public static void main(String[] args) {
        parseJsonToExcel("/Users/xxx/Desktop/jsonToExcel.xls", "data", mavenInfos);
    }

    static void parseJsonToExcel(String saveFileName, String rootNodeName, String sourceJson) {
        try {
            File filewrite = new File(saveFileName);
            filewrite.createNewFile();
            OutputStream os = new FileOutputStream(filewrite);
            JSONArray jsonArray = JSONArray.fromObject(sourceJson);
//            JSONObject jsonObject = JSONObject.fromObject(sourceJson);
            createExcelByJsonArray(os, jsonArray, rootNodeName);
//            log.info("解析完毕");
            System.out.println("解析完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public static void createExcel(OutputStream os, JSONObject apiJarInfos, String rootNodeName) throws WriteException, IOException {
//        //创建工作薄
//        WritableWorkbook workbook = Workbook.createWorkbook(os);
//        //创建新的一页
//        WritableSheet sheet = workbook.createSheet("First Sheet", 0);
//        JSONArray jsonArray = (JSONArray) apiJarInfos.get(rootNodeName);
//        JSONObject jsonObjectHeader = jsonArray.getJSONObject(0);
//        String[] headers = createTableHeader(jsonObjectHeader, sheet);
//        int size = jsonArray.size() + 1;
//        for (int i = 1; i < size; i++) {
//            JSONObject jsonObject = jsonArray.getJSONObject(i - 1);
//            int j = 0;
//            for (String key : headers) {
//                Label cellValue = new Label(j, i, jsonObject.get(key).toString());
//                sheet.addCell(cellValue);
//                j++;
//            }
//        }
//        //把创建的内容写入到输出流中，并关闭输出流
//        workbook.write();
//        workbook.close();
//        os.close();
//    }

    public static void createExcelByJsonArray(OutputStream os, JSONArray jsonArray, String rootNodeName) throws WriteException, IOException {
        //创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        //创建新的一页
        WritableSheet sheet = workbook.createSheet("First Sheet", 0);
//        JSONArray jsonArray = (JSONArray) apiJarInfos.get(rootNodeName);
        JSONObject jsonObjectHeader = jsonArray.getJSONObject(0);
        String[] headers = createTableHeader(jsonObjectHeader, sheet);
        int size = jsonArray.size() + 1;
        for (int i = 1; i < size; i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i - 1);
            int j = 0;
            for (String key : headers) {
                try {
                    Label cellValue = new Label(j, i, jsonObject.get(key).toString());
                    sheet.addCell(cellValue);
                    j++;
                } catch (Exception e) {
                    j++;
                    continue;
                }
            }
        }
        //把创建的内容写入到输出流中，并关闭输出流
        workbook.write();
        workbook.close();
        os.close();
    }


    static String[] createTableHeader(JSONObject jsonObjectHeader, WritableSheet sheet) throws WriteException {
        //遍历JSONObject中的key
        Iterator iterable = jsonObjectHeader.keys();
        String[] headers = new String[jsonObjectHeader.size()];
        int i = 0;
        while (iterable.hasNext()) {
            //创建要显示的内容,创建一个单元格，第一个参数为列坐标，第二个参数为行坐标，第三个参数为内容
            String headerName = iterable.next().toString();
            Label cell = new Label(i, 0, headerName);
            sheet.addCell(cell);
            headers[i] = headerName;
            i++;
//            log.info(headerName);
            System.out.println(headerName);
        }
        return headers;
    }

    private static final String mavenInfos = JsonConvert.getJsonString();
//            "{  \n" +
//            "  \"data\": [\n" +
//            "        {\n" +
//            "            \"groupId\": \"com.test.demo\",\n" +
//            "            \"artifactId\": \"demo-api\",\n" +
//            "            \"version\": \"1.0.0-release\",\n" +
//            "            \"latestSnapshot\": \"2.0.0-SNAPSHOT\"\n" +
//            "        },\n" +
//            "        {\n" +
//            "            \"groupId\": \"com.test.demo.core\",\n" +
//            "            \"artifactId\": \"demo-core\",\n" +
//            "            \"version\": \"1.1.3\",\n" +
//            "            \"latestSnapshot\": \"1.3.0-SNAPSHOT\"\n" +
//            "        }\n" +
//            "    ]\n" +
//            "}";
}
